﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Runtime.InteropServices.WindowsRuntime;
using System.Threading.Tasks;
using Newtonsoft.Json;
using SFOOD.Controllers;
using SFOOD.Model;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Media.Animation;
using Windows.UI.Xaml.Navigation;

// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=391641

namespace SFOOD
{
    /// <summary>
    /// An empty page that can be used on its own or navigated to within a Frame.
    /// </summary>
    public sealed partial class MainPage : Page
    {
        double initialPosition;
        bool _viewMoved = false;

        public MainPage()
        {
            this.InitializeComponent();

            this.NavigationCacheMode = NavigationCacheMode.Required;
            
        }

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            var data = HomeController.GetList().Result;
            foreach (var item in data.foods)
            {
                item.name = Cryto.Decrypt(item.name);
            }
            Home.DataContext = data;
            Hot.DataContext = data;
        }

        private void OpenClose_Left(object sender, RoutedEventArgs e)
        {
            var left = Canvas.GetLeft(LayoutRoot);
            if (left > -100)
            {
                MoveViewWindow(-340);
            }
            else
            {
                MoveViewWindow(0);
            }
        }
        private void OpenClose_Right(object sender, RoutedEventArgs e)
        {
            var left = Canvas.GetLeft(LayoutRoot);
            if (left > -450)
            {
                MoveViewWindow(-700);
            }
            else
            {
                MoveViewWindow(-350);

            }
        }

        void MoveViewWindow(double left)
        {
            _viewMoved = true;
            //((Storyboard)canvas.Resources["moveAnimation"]).SkipToFill();
            ((DoubleAnimation)((Storyboard)canvas.Resources["moveAnimation"]).Children[0]).To = left;
            ((Storyboard)canvas.Resources["moveAnimation"]).Begin();
        }

        private void canvas_ManipulationDelta(object sender, ManipulationDeltaRoutedEventArgs e)
        {
            if (e.Delta.Translation.X != 0)
                Canvas.SetLeft(LayoutRoot, Math.Min(Math.Max(-700, Canvas.GetLeft(LayoutRoot) + e.Delta.Translation.X), 0));//set khoang duoc phep vuot
        }

        private void canvas_ManipulationStarted(object sender, ManipulationStartedRoutedEventArgs e)
        {
            _viewMoved = false;
            initialPosition = Canvas.GetLeft(LayoutRoot);//danh dau vi tri dau ten khi vuot
        }

        private void canvas_ManipulationCompleted(object sender, ManipulationCompletedRoutedEventArgs e)
        {
            var left = Canvas.GetLeft(LayoutRoot);
            if (_viewMoved)
                return;
            if (Math.Abs(initialPosition - left) < 60)
            {
                //bouncing back
                MoveViewWindow(initialPosition);
                return;
            }
            //change of state
            if (initialPosition - left > 0)
            {
                //slide to the left
                if (initialPosition > -350)
                    MoveViewWindow(-350);
                else
                    MoveViewWindow(-700);
            }
            else
            {
                //slide to the right
                if (initialPosition < -350)
                    MoveViewWindow(-350);
                else
                    MoveViewWindow(0);
            }

        }

    }
}
